package net.yla.smartbus.core.util;

import net.yla.smartbus.core.api.RequestCode;
import net.yla.smartbus.core.exception.AppException;
import net.yla.smartbus.module.ba.utils.mapper.SignatureAuthenicationUtilMapper;
import net.yla.smartbus.module.ba.utils.service.SignatureAuthenicationUtilService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 *@author:LiuJie
 *@date:2020/8/12
 *@description:接口鉴权模块
 */
@Component
public class SignatureAuthenticationUtil {

    @Autowired
    SignatureAuthenicationUtilMapper signatureAuthenicationUtilMapper;

    public int  SignatureAuthentication(String ak,String body,String sign)throws Exception{
        //校验ak是否为空
        if (StringUtils.isBlank(ak)){
            throw new AppException("用户未登录", RequestCode.Not_Login);
        }
        //拿到ak数据库请求平台表获取sk
        String sk = signatureAuthenicationUtilMapper.selectSkByAk(ak);
        if (StringUtils.isBlank(sk)){
            throw new AppException("用户未登录", RequestCode.Not_Login);
        }
        String parameter = MD5Util.MD5(sk+body);
        if (parameter.equals(sign)){
            return 1;
        }
        return 0;
    }
}
